// ========================================================
// Des：
// Autor：师哥吃你个小鸡腿 
// Email：hf_8220@163.com 
// CreateTime：2024/01/15 22:26:10
// 版 本：v 1.0
// ========================================================
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : NetworkBehaviour
{
    //public static GameManager Instance;
    private Button btn_Host;
    private Button btn_Server;
    private Button btn_Client;
    private Button btn_ShutDown;
    private Button btn_Game;
    private GameObject canvas;

    public override void OnNetworkSpawn()
    {
        base.OnNetworkSpawn();


        NetworkManager.Singleton.OnClientConnectedCallback += (id) =>
        {
            Debug.Log("a new client connect id: " + id);
        };
        NetworkManager.Singleton.OnClientDisconnectCallback += (id) =>
        {
            Debug.Log("a  client disconnect id: " + id);
        };
        NetworkManager.Singleton.OnServerStarted += () =>
        {
            Debug.Log("start server");
        };
    }
    // Start is called before the first frame update
    void Start()
    {
        //Instance = this;
        DontDestroyOnLoad(gameObject);
        canvas = GameObject.Find("Canvas");
        btn_Host = canvas.transform.Find("Btn_Host").GetComponent<Button>();
        btn_Server = canvas.transform.Find("Btn_Server").GetComponent<Button>();
        btn_Client = canvas.transform.Find("Btn_Client").GetComponent<Button>();
        btn_ShutDown = canvas.transform.Find("Btn_ShutDown").GetComponent<Button>();
        btn_Game = canvas.transform.Find("Btn_Game").GetComponent<Button>();

        btn_Host.onClick.AddListener(() =>
        {
            if (NetworkManager.Singleton.StartHost())
            {
                Debug.Log("start host suc");
            }
            else
            {
                Debug.Log("host error");
            }
        });
        btn_Server.onClick.AddListener(() =>
        {
            if (NetworkManager.Singleton.StartServer())
            {
                Debug.Log("start server suc");
            }
            else
            {
                Debug.Log("server error");
            }
        });
        btn_Client.onClick.AddListener(() =>
        {
            if (NetworkManager.Singleton.StartClient())
            {
                Debug.Log("start client suc");
            }
            else
            {
                Debug.Log("client error");
            }
        });
        btn_ShutDown.onClick.AddListener(() =>
        {
            NetworkManager.Singleton.Shutdown();
            Debug.Log("shut down");

        });
        btn_Game.onClick.AddListener(() =>
       {

           LoadScene("Game");

       });
    }

    private void LoadScene(string sceneName)
    {
        NetworkManager.Singleton.SceneManager.LoadScene("Game", LoadSceneMode.Single);
        Debug.Log("load game scene");
    }

}
